using IdentityServer4.AccessTokenValidation;

using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;

using Ocelot.Cache.CacheManager;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Ocelot.Provider.Polly;
using Ocelot.Values;
var builder = WebApplication.CreateBuilder(args);
IdentityModelEventSource.ShowPII = true;
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);//�����ȥ���json�ļ�����ᵼ�²�����,����Ҳ�Ǳ�Ҫ��
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1",
        new OpenApiInfo { Title = "Gateway API", Version = "v1", Description = "#gateway api..." });
});
builder.Services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
       .AddIdentityServerAuthentication("orderService", options =>
       {

           options.Authority = builder.Configuration.GetValue<string>("Authority");//"https://docker.for.win.localhost:7128";// "http://docker.for.win.localhost:5065";//��Ȩ���ĵ�ַ
           options.ApiName = "orderApi";
           options.SupportedTokens = SupportedTokens.Both;
           options.ApiSecret = "orderApisecret";
           options.RequireHttpsMetadata = false;

           //options.SaveToken = true;
       })
       .AddIdentityServerAuthentication("productService", options =>
       {
           options.Authority = builder.Configuration.GetValue<string>("Authority");// "https://docker.for.win.localhost:7128";// "https://docker.for.win.localhost:7128"; //"http://docker.for.win.localhost:5065";//��Ȩ���ĵ�ַ
           options.ApiName = "productApi";
           options.SupportedTokens = SupportedTokens.Both;
           options.ApiSecret = "productApisecret";
           options.RequireHttpsMetadata = false;

           // options.SaveToken = true;
       });


builder.Services.AddOcelot()
    //���consul
    .AddConsul()
  //   ��ӻ���
    .AddCacheManager(x =>
    {//ocelot�����ļ���������Ч
        x.WithDictionaryHandle();
    })
    //���polly ok
    .AddPolly();

builder.Services.AddLogging(builder =>
{
    builder.AddFilter("Ocelot", LogLevel.Warning);
    builder.AddLog4Net();
});
var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{ }
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/order/swagger/v1/swagger.json", "Order API V1");
    c.SwaggerEndpoint("/product/swagger/v1/swagger.json", "Product API V1");
});


app.UseOcelot().Wait();

app.UseHttpsRedirection();


app.UseAuthorization();

app.MapControllers();


app.Run();
